var dayjs = require("dayjs");
import { BASE_URL } from "@/api/http.js";
const IDCardFormat = (ID) => {
  const reg = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/;
  if (reg.test(ID)) {
    let str = ID.slice(0, 3);
    str += "***";
    str += ID.slice(ID.length - 2);
    return str;
  } else {
    return ID;
  }
};

const imgFormat = (img, isCar) => {
  if (isCar) {
    if (!img || img === BASE_URL) {
      return require("@/static/images/common/car.png");
    }
    return img;
  } else {
    if (!img || img === BASE_URL) {
      return require("@/static/images/index/avatar.png");
    }
    return img;
  }
};

const weatherFormat = (str) => {
  if (str === "CLEAR_DAY" || str === "CLEAR_NIGHT") {
    return require("@/static/images/weather/clear_day.svg");
  } else if (str === "PARTLY_CLOUDY_DAY" || str === "PARTLY_CLOUDY_NIGHT") {
    return require("@/static/images/weather/partly_cloudy_day.svg");
  } else if (str === "CLOUDY") {
    return require("@/static/images/weather/cloudy.svg");
  } else if (str === "LIGHT_HAZE") {
    return require("@/static/images/weather/light_haze.svg");
  } else if (str === "MODERATE_HAZE") {
    return require("@/static/images/weather/moderate_haze.svg");
  } else if (str === "HEAVY_HAZE") {
    return require("@/static/images/weather/heavy_haze.svg");
  } else if (str === "LIGHT_RAIN") {
    return require("@/static/images/weather/light_rain.svg");
  } else if (str === "MODERATE_RAIN") {
    return require("@/static/images/weather/moderate_rain.svg");
  } else if (str === "HEAVY_RAIN") {
    return require("@/static/images/weather/heavy_rain.svg");
  } else if (str === "STORM_RAIN") {
    return require("@/static/images/weather/storn_rain.svg");
  } else if (str === "FOG") {
    return require("@/static/images/weather/fog.svg");
  } else if (str === "SNOW") {
    return require("@/static/images/weather/snow.svg");
  } else if (str === "LIGHT_SNOW") {
    return require("@/static/images/weather/light_snow.svg");
  } else if (str === "MODERATE_SNOW") {
    return require("@/static/images/weather/moderate_snow.svg");
  } else if (str === "HEAVY_SNOW") {
    return require("@/static/images/weather/heavy_snow.svg");
  } else if (str === "STORM_SNOW") {
    return require("@/static/images/weather/storm_snow.svg");
  } else if (str === "DUST") {
    return require("@/static/images/weather/dust.svg");
  } else if (str === "SAND") {
    return require("@/static/images/weather/sand.svg");
  } else if (str === "WIND") {
    return require("@/static/images/weather/wind.svg");
  }
};

const weatherInfoFormat = (str) => {
  if (str === "CLEAR_DAY" || str === "CLEAR_NIGHT") {
    return "晴天";
  } else if (str === "PARTLY_CLOUDY_DAY" || str === "PARTLY_CLOUDY_NIGHT") {
    return "多云";
  } else if (str === "CLOUDY") {
    return "阴";
  } else if (str === "LIGHT_HAZE") {
    return "轻度雾霾";
  } else if (str === "MODERATE_HAZE") {
    return "中度雾霾";
  } else if (str === "HEAVY_HAZE") {
    return "重度雾霾";
  } else if (str === "LIGHT_RAIN") {
    return "小雨";
  } else if (str === "MODERATE_RAIN") {
    return "中雨";
  } else if (str === "HEAVY_RAIN") {
    return "大雨";
  } else if (str === "STORM_RAIN") {
    return "暴雨";
  } else if (str === "FOG") {
    return "雾";
  } else if (str === "LIGHT_SNOW") {
    return "小雪";
  } else if (str === "MODERATE_SNOW") {
    return "中雪";
  } else if (str === "HEAVY_SNOW") {
    return "大雪";
  } else if (str === "STORM_SNOW") {
    return "暴雪";
  } else if (str === "DUST") {
    return "浮尘";
  } else if (str === "SAND") {
    return "沙尘";
  } else if (str === "WIND") {
    return "大风";
  } else {
    return "";
  }
};

// arg表示原始参数,num表示小数点位,默认保留两位,symbol表示符号（￥,$）,spilt是否进行逗号分隔【true，false】
const priceFormat = (arg, num = 0, symbol, spilt = false) => {
  arg = Number(arg).toFixed(num);
  if (spilt == true) {
    if (arg.indexOf(".") == -1) {
      arg = arg.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    } else {
      var parts = arg.split(".");
      parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      arg = parts.join(".");
    }
  }

  if (symbol) {
    arg = symbol + arg;
  }
  return arg;
};

const dayFormat = (time) => {
  const d = new Date(time);
  const day = d.getDay();
  const newD = +new Date();
  const oldD = +new Date(time);
  if (oldD - newD <= 86400000) {
    return "明天";
  }
  let days = "";
  switch (day) {
    case 0:
      days = "星期日";
      break;
    case 1:
      days = "星期一";
      break;
    case 2:
      days = "星期二";
      break;
    case 3:
      days = "星期三";
      break;
    case 4:
      days = "星期四";
      break;
    case 5:
      days = "星期五";
      break;
    case 6:
      days = "星期六";
      break;
  }
  return days;
};

const timeFormat = (time, format) => {
  if (time) {
    return dayjs(time).format(format);
  } else {
    return time;
  }
};

export {
  IDCardFormat,
  imgFormat,
  weatherFormat,
  priceFormat,
  dayFormat,
  timeFormat,
  weatherInfoFormat,
};
